package com.egu.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.egu.common.util.DateUtil;
import com.egu.common.util.UserUtil;
import com.egu.mapper.CustomerCareMapper;
import com.egu.model.Customer;
import com.egu.model.CustomerCare;
import com.egu.model.CustomerCareExample;
import com.egu.model.CustomerCareExample.Criteria;
import com.egu.model.User;
import com.egu.service.CustomerCareService;
import com.egu.service.CustomerService;
import com.egu.vo.PageParam;
import com.egu.vo.RequestPageVo;
import com.egu.vo.RequestResultVo;

@Service
public class CustomerCareServiceImpl implements CustomerCareService {
	@Autowired
	CustomerCareMapper customerCareMapper;
	@Autowired
	CustomerService customerService;
	@Override
	public RequestPageVo<CustomerCare> getList(PageParam param, CustomerCare customerCare) {
		RequestPageVo<CustomerCare> result = new RequestPageVo<CustomerCare>();
		CustomerCareExample example = new CustomerCareExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsUsedEqualTo("1");
		User currentUser = UserUtil.getCurrentUser();
		if(currentUser.getRolePower()==1) {
			criteria.andCarePeopleEqualTo(currentUser.getUserName());
		}
		
		if (StringUtils.isNotBlank(customerCare.getCustomerName())) {
			criteria.andCustomerNameLike("%"+customerCare.getCustomerName()+"%");
		}

		int count = customerCareMapper.countByExample(example);
		if (count > 0) {
			example.setStartPage(param.getOffset());
			example.setPageSize(param.getLimit());
			List<CustomerCare> list = customerCareMapper.selectByExample(example);
			if(list.size()>0) {
				for (CustomerCare customerCare2 : list) {
					Integer id = customerCare2.getCustomerId();
					Customer customer = customerService.getCustomertById(id);
					customerCare2.setCustomerName(customer.getCustomerName());
				}
			}
			result.setRows(list);
			result.setTotal(count);
		}
		return result;
	}

	@Override
	@Transactional
	public RequestResultVo<CustomerCare> saveOrUpdate(CustomerCare customerCare) {
		User currentUser = UserUtil.getCurrentUser();
		if (customerCare.getId() == null) {
			Customer customer = customerService.getCustomertById(customerCare.getCustomerId());
			customerCare.setCustomerName(customer.getCustomerName());
			customerCare.setCarePeople(currentUser.getUserName());
			customerCareMapper.insertSelective(customerCare);
		} else {
			customerCareMapper.updateByPrimaryKeySelective(customerCare);
		}
		return RequestResultVo.success();
	}

	@Override
	@Transactional
	public RequestResultVo<CustomerCare> deleteDataById(Integer id) {
		CustomerCare customerCare = new CustomerCare();
		customerCare.setId(id);
		customerCare.setIsUsed("0");
		try {
			customerCareMapper.updateByPrimaryKeySelective(customerCare);
		} catch (Exception e) {
			return RequestResultVo.error("数据库删除失败");
		}
		return RequestResultVo.success();
	}

	@Override
	public List<CustomerCare> getRecentList() {
		CustomerCareExample example = new CustomerCareExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsUsedEqualTo("1");
		criteria.andCareNexttimeGreaterThan(DateUtil.getCurrentDate());
		List<CustomerCare> list = customerCareMapper.selectByExample(example);
		return list;
	}

}
